<?php

namespace Modules\Asset\Http\Controllers\Client\V1;

use Illuminate\Http\Request;
use Illuminate\Routing\Controller;

use Modules\Asset\Entities\WithdrawRecord;

use Validator;
use ThrowException;
use Auth;
use Setting;

class WithdrawController extends Controller
{	
	public function detail(Request $req)
	{
		$setting = @Setting::get('sys_redpack') ?: [];

		$info = [
			'setting' => $setting
		];

		return $info;
	}

    public function index(Request $req)
    {
        $rule = [
        ];

        Validator::make($req->all(), $rule)->fails() && ThrowException::BadRequest();

        $user = Auth::requireLoginUser();

        $assetName = 'redpack';
        in_array($assetName, array_keys(config('asset.map'))) || ThrowException::Conflict('错误的资产类型');

        $options = $req->all();
        $options['builder'] = $user->asset('redpack')->withdraw_records();

        $options['only'] = [
            'id',
            'uuid',
            'number',
            'value',
            'withdraw_type',
            'account',
            'union_status',
            'union_status_text',
            'bank_name',
            'bank_account_name',
            'bank_account_number',
            'created_at',
        ];

        $list = WithdrawRecord::getList($options);

        return $list->generateListResponse();
    }


    public function store(Request $req)
    {
        $rule = [
            'value' => ['required'],
            'withdraw_type' => ['required']
        ];

        Validator::make($req->all(), $rule)->fails() && ThrowException::BadRequest();

        $user = Auth::requireLoginUser();

        // 强制要求绑定手机号
        if (!$user->phone) {
        	ThrowException::RequirePhone();
        }

        $assetName = 'redpack';

        // 红包提现
        if ($assetName === 'redpack') {
        	$setting = @Setting::get('sys_redpack') ?: [];
            @Setting::get('sys_redpack')['is_withdraw_enabled'] ?: ThrowException::Conflict('余额提现功能暂不开放~');

            $userGroupId = @$setting['user_group_id'];
            if ($userGroupId) {
                $user->isInUserGroup($userGroupId) ?: ThrowException::Conflict('暂不符合提现条件~');
            }
            // $hour = @$setting['min_register_time'];
            // if ($hour) {
            // 	$line = date('Y-m-d H:i:s', strtotime('-' . $hour . ' hours'));
            // 	if ($user->created_at > $line) {
            // 		ThrowException::Conflict('注册时间超过' . $hour . '小时后才能申请提现~');
            // 	}
            // }

            $minWithdraw = @Setting::get('sys_redpack')['min_withdraw'] ?: 0;
            $maxWithdraw = @Setting::get('sys_redpack')['max_withdraw'] ?: 0;
            if ($req->value < $minWithdraw) {
                ThrowException::Conflict('最低提现金额为:' . ($minWithdraw / 100) . '元');
            }
            if ($maxWithdraw && $req->value > $maxWithdraw) {
                ThrowException::Conflict('单次最高提现金额为:' . ($maxWithdraw / 100) . '元');
            }

            $options = $req->all();

            if ($req->input('bank_card_id')) {
                $card = $user->bank_cards()->where('id', $req->bank_card_id)->first();

                if (!$card) {
                    ThrowException::Conflict('银行卡不存在~');
                }

                $options['bank_account_name']  = $card->bank_account_name;
                $options['bank_account_number'] = $card->bank_account_number;
                $options['bank_name'] = $card->bank_name;
                $options['account'] = $card->bank_account_name . ' ' . $card->bank_account_number . ' ' . $card->bank_name;
            }

            // 没有填帐号
            if (!@$options['account']) {
                $options['account'] = $req->input('account_number') . ' ' . $req->input('account_name');
            }

            $item = $user->asset($assetName)->withdraw($options);
        }

        return [
            'id' => $item->id,
            'uuid' => $item->uuid
        ];
    }

}
